<template>
  <div>
    <div>
      <el-dialog title="编辑用户" :visible.sync="$props.visible" width="50%" @close="handleClose">

        <div class="add-content">
          <el-form
            ref="formName"
            :model="staffFrom"
            :rules="staffRules"
            label-width="120px"
            style="margin-top: 10px;"
          >
            <el-form-item label="用户类型：" prop="type">
              <el-input v-model="staffFrom.type" disabled style="width: 60%;" />
            </el-form-item>
            <el-form-item label="所属角色：" prop="roleIds">
              <el-select v-model="staffFrom.roleIds" disabled placeholder="请选择" style="width: 60%;">
                <el-option
                  v-for="item in rolesList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="手机：" prop="mobile">
              <el-input v-model="staffFrom.mobile" disabled style="width: 60%;" />
            </el-form-item>
            <el-form-item label="姓名：" prop="name">
              <el-input v-model="staffFrom.name" placeholder="请输入姓名" style="width: 60%;" />
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input v-model="staffFrom.password" show-password placeholder="密码留空则不修改密码" style="width: 60%;" />
            </el-form-item>
            <el-form-item label="账号状态：" prop="status">
              <el-radio v-model="staffFrom.status" label="ACTIVE">启用</el-radio>
              <el-radio v-model="staffFrom.status" label="FROZEN">禁用</el-radio>
            </el-form-item>
          </el-form>
        </div>

        <span class="dialog-footer">
          <div style="margin-top:15px;">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="submitClick">提交</el-button>
          </div>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { editUser, getRolesListByUserId } from '@/api/iam/iamUser'
import * as R from 'ramda'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    userTypesMap: {
      type: Map,
      default: {}
    },
    rolesList: {
      type: Array,
      default() {
        return []
      }
    },
    selectedRow: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      staffFrom: {
        type: '',
        roleIds: '',
        name: '',
        password: '',
        status: ''
      },
      staffRules: {
        name: [{ required: true, message: '请填写姓名', trigger: 'blur' }],
        status: [{ required: true, message: '请选择用户状态', trigger: 'blur' }]
      },
      userRoles: []
    }
  },
  created() {
    getRolesListByUserId(this.selectedRow.id).then((response) => {
      this.userRoles = response
      this.staffFrom = {
        type: this.userTypesMap.get(this.selectedRow.type).name,
        roleIds: R.join(',', R.map(role => role.id, response)),
        mobile: this.selectedRow.mobile,
        name: this.selectedRow.name,
        password: '',
        status: this.selectedRow.status
      }
    })
  },
  methods: {
    // 弹出框关闭后触发
    handleClose() {
      // 子组件调用父组件方法，并传递参数
      this.$emit('handleClose')
    },
    handleSuccess() {
      this.$message({
        message: '修改成功',
        type: 'success'
      })
      this.handleClose()
      this.$emit('handleSuccess')
    },
    // 新增
    submitClick() {
      this.$refs['formName'].validate((valid) => {
        if (valid) {
          const roleIds = [this.staffFrom.roleIds.toString()]
          const data = {
            'roleIds': roleIds,
            'mobile': this.staffFrom.mobile,
            'name': this.staffFrom.name,
            'password': this.staffFrom.password,
            'status': this.staffFrom.status
          }
          editUser(this.selectedRow.id, data).then((response) => {
            this.handleSuccess()
          })
        }
      })
    }
  }
}
</script>
<style>
.add-content {
    border: 1px solid #e4e4e4;
    width: 90%;
    margin: 0 auto;
    height: 300px;
}
.dialog-footer{
    width: 97%;
    display: flex;
    justify-content:center;
}
</style>
